package com.atjg.autocat.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.atjg.autocat.R
import com.atjg.autocat.bean.GoodBean
import com.atjg.autocat.viewModel.MainViewModel
import com.blankj.utilcode.util.ScreenUtils
import com.bumptech.glide.Glide
import com.lxj.xpopup.core.CenterPopupView
import com.zhy.baselibrary.expand.ToastMgr
import com.zhy.baselibrary.utils.clickWithTrigger
import kotlinx.coroutines.Job


@SuppressLint("ViewConstructor")
class GoodDialog(
    val mContext: Context,
    val item: GoodBean,
    private val onGoodSumListener: OnGoodSumListener
) : CenterPopupView(mContext) {
    private var count = 1

    private val viewModel by lazy { MainViewModel() }
    private var job: Job? = null
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_good
    }

    @SuppressLint("SetTextI18n", "DefaultLocale")
    override fun onCreate() {
        super.onCreate()
        val tvTime = findViewById<TextView>(R.id.tvTime)
        job = viewModel.testTime(30000, {
            tvTime.text = "兑换倒计时：${it}秒"
            if (it == 0L) {
                dismiss()
            }
        }, {
            dismiss()
        })

        val tvGoodName = findViewById<TextView>(R.id.tvGoodName)
        if (item.goodsName.isNullOrEmpty()) {
            tvGoodName.visibility = View.GONE
        } else {
            tvGoodName.text = item.goodsName
            tvGoodName.visibility = View.VISIBLE
        }
        val ivGood = findViewById<ImageView>(R.id.ivGood)
        Glide.with(mContext).load(item.photoUrl).into(ivGood)
        val tvInfo = findViewById<TextView>(R.id.tvInfo)
        val jifen = if (item.currentJifen > 0) {
            item.currentJifen
        } else {
            item.jifen
        }
        tvInfo.text =
            "单价：${String.format("%.2f", jifen)}元，剩余：${item.currentGoodsSum}"
        val tvAll = findViewById<TextView>(R.id.tvAll)
        tvAll.text = "合计：${String.format("%.2f", item.jifen.toDouble())}元"

        findViewById<TextView>(R.id.tvWeChatPay).clickWithTrigger(1000) {
            if (item.currentGoodsSum < count) {
                ToastMgr.show("库存不足")
                return@clickWithTrigger
            }
            val jifen = if (item.currentJifen > 0) {
                item.currentJifen
            } else {
                item.jifen
            }
            onGoodSumListener.onQR(item, count, String.format("%.2f", jifen * count))
            job?.cancel()
            dismiss()
        }
        findViewById<TextView>(R.id.tvCancel).clickWithTrigger {
            job?.cancel()
            dismiss()
        }
    }

    override fun getMaxWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }

    override fun dismiss() {
        super.dismiss()
        job?.cancel()
    }

    interface OnGoodSumListener {
        fun onClick(item: GoodBean, count: Int, sumNum: String)
        fun onQR(item: GoodBean, count: Int, sumNum: String)
    }
}